{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5. Transformation rules extraction\n",
    "\n",
    "(c) 2019, Dr. Ramil Nugmanov; Dr. Timur Madzhidov; Ravil Mukhametgaleev\n",
    "\n",
    "Installation instructions of CGRtools package information and tutorial's files see on `https://github.com/cimm-kzn/CGRtools`\n",
    "\n",
    "NOTE: Tutorial should be performed sequentially from the start. Random cell running will lead to unexpected results. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pkg_resources\n",
    "if pkg_resources.get_distribution('CGRtools').version.split('.')[:2] != ['3', '1']:\n",
    "    print('WARNING. Tutorial was tested on 3.1 version of CGRtools')\n",
    "else:\n",
    "    print('Welcome!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# load data for tutorial\n",
    "from pickle import load\n",
    "from traceback import format_exc\n",
    "\n",
    "with open('molecules.dat', 'rb') as f:\n",
    "    molecules = load(f) # list of MoleculeContainer objects\n",
    "with open('reactions.dat', 'rb') as f:\n",
    "    reactions = load(f) # list of ReactionContainer objects\n",
    "\n",
    "m3 = molecules[2]\n",
    "m7 = m3.copy()\n",
    "m7.standardize()\n",
    "r1 = reactions[0] # reaction\n",
    "m8 = m7.substructure([4, 5, 6, 7, 8, 9], as_view=False)\n",
    "cgr1 = m7 ^ m8 \n",
    "cgr1.reset_query_marks() \n",
    "\n",
    "from CGRtools.containers import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "CGRtools can be used to generate molecules and reactions based on a given transformation rule.\n",
    "\n",
    "**How to extract transformation rule**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cgr1.center_atoms # list of atom numbers of reaction center. If several centers exist they will also be added to this list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cgr1.center_bonds # list of dynamic bonds as tuples of adjacent atom numbers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cgr1.centers_list # list of lists of atom numbers belonging to each reaction center. \n",
    "                  # Distant reaction centers will be split into separate lists"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rc1 = cgr1.substructure([13, 7]) # get reaction center from CGR\n",
    "format(rc1, 'hn')               # Notice that query marks are set."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "rc1 is phenol reduction, phenol is transformed into unsubstituted benzene:\n",
    "\n",
    "* \\[C;a3;\\]-aromatic carbon with 3 neighbors\n",
    "* \\[O;s1;\\]-oxygen with 1 neighbor\n",
    "* \\[C;a2;\\]-carbon with 2 neighbors\n",
    "* \\[O;s0;\\]-oxygen without neighbors (water). It probably appears since initial reaction was unbalanced."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rule = QueryCGRContainer(rc1) # transform reaction into query to take query"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(rule) # all query marks are on their place. Without them generation will be too unrestrictive. \n",
    "            # If needed CGRtools could be used to include atomic environment, etc..."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "cgrtools",
   "language": "python",
   "name": "cgrtools"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
